package com.zhixianggou.caibaobox.lottery.dataCollect;

import com.zhixianggou.caibaobox.lottery.dataCollect.cover.SLotteryCover;
import com.zhixianggou.caibaobox.lottery.dataCollect.cover.WLotteryCover;
import com.zhixianggou.caibaobox.lottery.dataCollect.dao.*;
import com.zhixianggou.caibaobox.lottery.dataCollect.dto.SLotteryDto;
import com.zhixianggou.caibaobox.lottery.dataCollect.dto.SLotteryPageDto;
import com.zhixianggou.caibaobox.lottery.dataCollect.dto.WLotteryDto;
import com.zhixianggou.caibaobox.lottery.dataCollect.dto.WLotteryPageDto;
import com.zhixianggou.caibaobox.lottery.dataCollect.entities.*;
import com.zhixianggou.caibaobox.lottery.dataCollect.utils.DataCollectUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author zyq
 * 8/22/24 12:08 PM
 */
@Component
public class SportsLotteryCollectService {

   @Resource
   RestTemplate restTemplate;

   @Resource
   SDaLeTouMapper sDaLeTouMapper;

   @Resource
   SArray3Mapper sArray3Mapper;

   @Resource
   SArray5Mapper sArray5Mapper;

   @Resource
   SSevenStarMapper sSevenStarMapper;

   @Resource
   WSsqMapper wSsqMapper;

   @Resource
   WKl8Mapper wKl8Mapper;

   @Resource
   W3dMapper w3dMapper;

   @Resource
   WQlcMapper wQlcMapper;

   /**
    * 收集数据--体彩大乐透
    */
   public void getData4DALETOU()
   {
      //获取db中的最大期数
      SDaLeTou sDaLeTou = sDaLeTouMapper.selectMaxTerm();
      String startTerm = sDaLeTou == null ? null : sDaLeTou.getLotteryDrawNum();
      String endTerm = null;
      Integer total = 0;

      //获取网站最近一期数据
      SLotteryPageDto lastDataPageDto = DataCollectUtil.getList4SLottery(restTemplate,DataCollectUtil.SGameNo.S_DALETOU.getNo(),1, 1);
      endTerm = lastDataPageDto.getLastPoolDraw().getLotteryDrawNum();
      total = lastDataPageDto.getTotal();

      int pageSize = 30;
      //由于网站数据查询结果是倒序列表，我们需要从最后页开始往前查询
      //根据总条数和每页条数计算总页数
      int pageNo = (total / pageSize) + (total % pageSize == 0 ? 0 : 1);

      List<SDaLeTou> importList = new ArrayList<>();
      while (pageNo > 0) {
         SLotteryPageDto pageDto = DataCollectUtil.getList4SLottery(restTemplate,DataCollectUtil.SGameNo.S_DALETOU.getNo(), pageNo, pageSize, startTerm, endTerm);
         List<SLotteryDto> list = pageDto.getList();
         List<SDaLeTou> poList = SLotteryCover.INSTANCE.toDaLeTouPo(list);
         if(startTerm == null){
            importList.addAll(poList);
         }else {
            importList.addAll(poList.stream().filter(po -> po.getLotteryDrawNum().compareTo(startTerm) > 0).collect(Collectors.toList()));
         }
         if(CollectionUtils.isEmpty(importList)){
            break;
         }
         //正序排序后导入数据库
         sDaLeTouMapper.insertBatch(importList.stream().sorted(Comparator.comparing(SDaLeTou::getLotteryDrawNum)).collect(Collectors.toList()));
         pageNo --;
         importList.clear();
      }
   }

   /**
    * 收集数据--体彩排列三
    */
   public void getData4ARRAY3()
   {
      //获取db中的最大期数
      SArray3 sArray3 = sArray3Mapper.selectMaxTerm();
      String startTerm = sArray3 == null ? null : sArray3.getLotteryDrawNum();
      String endTerm = null;
      Integer total = 0;

      //获取网站最近一期数据
      SLotteryPageDto lastDataPageDto = DataCollectUtil.getList4SLottery(restTemplate,DataCollectUtil.SGameNo.S_ARRAY3.getNo(), 1, 1);
      endTerm = lastDataPageDto.getLastPoolDraw().getLotteryDrawNum();
      total = lastDataPageDto.getTotal();

      int pageSize = 30;
      //由于网站数据查询结果是倒序列表，我们需要从最后页开始往前查询
      //根据总条数和每页条数计算总页数
      int pageNo = (total / pageSize) + (total % pageSize == 0 ? 0 : 1);

      List<SArray3> importList = new ArrayList<>();
      while (pageNo > 0) {
         SLotteryPageDto pageDto = DataCollectUtil.getList4SLottery(restTemplate,DataCollectUtil.SGameNo.S_ARRAY3.getNo(), pageNo, pageSize, startTerm, endTerm);
         List<SLotteryDto> list = pageDto.getList();
         List<SArray3> poList = SLotteryCover.INSTANCE.toArray3Po(list);
         if(startTerm == null){
            importList.addAll(poList);
         }else {
            importList.addAll(poList.stream().filter(po -> po.getLotteryDrawNum().compareTo(startTerm) > 0).collect(Collectors.toList()));
         }
         if(CollectionUtils.isEmpty(importList)){
            break;
         }
         //正序排序后导入数据库
         sArray3Mapper.insertBatch(importList.stream().sorted(Comparator.comparing(SArray3::getLotteryDrawNum)).collect(Collectors.toList()));
         pageNo --;
         importList.clear();
      }
   }

   /**
    * 收集数据--体彩排列五
    */
   public void getData4ARRAY5()
   {
      //获取db中的最大期数
      SArray5 sArray5 = sArray5Mapper.selectMaxTerm();
      String startTerm = sArray5 == null ? null : sArray5.getLotteryDrawNum();
      String endTerm = null;
      Integer total = 0;

      //获取网站最近一期数据
      SLotteryPageDto lastDataPageDto = DataCollectUtil.getList4SLottery(restTemplate,DataCollectUtil.SGameNo.S_ARRAY5.getNo(), 1, 1);
      endTerm = lastDataPageDto.getLastPoolDraw().getLotteryDrawNum();
      total = lastDataPageDto.getTotal();

      int pageSize = 30;
      //由于网站数据查询结果是倒序列表，我们需要从最后页开始往前查询
      //根据总条数和每页条数计算总页数
      int pageNo = (total / pageSize) + (total % pageSize == 0 ? 0 : 1);

      List<SArray5> importList = new ArrayList<>();
      while (pageNo > 0) {
         SLotteryPageDto pageDto = DataCollectUtil.getList4SLottery(restTemplate,DataCollectUtil.SGameNo.S_ARRAY5.getNo(), pageNo, pageSize, startTerm, endTerm);
         List<SLotteryDto> list = pageDto.getList();
         List<SArray5> poList = SLotteryCover.INSTANCE.toArray5Po(list);
         if(startTerm == null){
            importList.addAll(poList);
         }else {
            importList.addAll(poList.stream().filter(po -> po.getLotteryDrawNum().compareTo(startTerm) > 0).collect(Collectors.toList()));
         }
         if(CollectionUtils.isEmpty(importList)){
            break;
         }
         //正序排序后导入数据库
         sArray5Mapper.insertBatch(importList.stream().sorted(Comparator.comparing(SArray5::getLotteryDrawNum)).collect(Collectors.toList()));
         pageNo --;
         importList.clear();
      }
   }

   /**
    * 收集数据--体彩七星彩
    */
   public void getData4SevenStar()
   {
      //获取db中的最大期数
      SSevenStar sSevenStar = sSevenStarMapper.selectMaxTerm();
      String startTerm = sSevenStar == null ? null : sSevenStar.getLotteryDrawNum();
      String endTerm = null;
      Integer total = 0;

      //获取网站最近一期数据
      SLotteryPageDto lastDataPageDto = DataCollectUtil.getList4SLottery(restTemplate,DataCollectUtil.SGameNo.S_SEVENSTAR.getNo(), 1, 1);
      endTerm = lastDataPageDto.getLastPoolDraw().getLotteryDrawNum();
      total = lastDataPageDto.getTotal();

      int pageSize = 30;
      //由于网站数据查询结果是倒序列表，我们需要从最后页开始往前查询
      //根据总条数和每页条数计算总页数
      int pageNo = (total / pageSize) + (total % pageSize == 0 ? 0 : 1);

      List<SSevenStar> importList = new ArrayList<>();
      while (pageNo > 0) {
         SLotteryPageDto pageDto = DataCollectUtil.getList4SLottery(restTemplate,DataCollectUtil.SGameNo.S_SEVENSTAR.getNo(), pageNo, pageSize, startTerm, endTerm);
         List<SLotteryDto> list = pageDto.getList();
         List<SSevenStar> poList = SLotteryCover.INSTANCE.toSevenStarPo(list);
         if(startTerm == null){
            importList.addAll(poList);
         }else {
            importList.addAll(poList.stream().filter(po -> po.getLotteryDrawNum().compareTo(startTerm) > 0).collect(Collectors.toList()));
         }
         if(CollectionUtils.isEmpty(importList)){
            break;
         }
         //正序排序后导入数据库
         sSevenStarMapper.insertBatch(importList.stream().sorted(Comparator.comparing(SSevenStar::getLotteryDrawNum)).collect(Collectors.toList()));
         pageNo --;
         importList.clear();
      }
   }





}
